#include "xcommand.h"
#include <stdio.h>
#include <string.h>
#include "xcommandtransaction.h"
#include "xmessage.h"
#include "xheader.h"
#include "msg.h"
#include "xrequest.h"
#include "xsmallrequest.h"
#include "xtransport.h"
#include "libxsip.h"

XSIPCommand::XSIPCommand()
{

}

XSIPCommand::~XSIPCommand()
{

}

void* XSIPCommand::getObject(const char* name) const
{
	if (strcmp(name, "XSIPCommand") == 0)
		return const_cast<XSIPCommand*>(this);
	else
		return XSIPCall::getObject(name);
}

XSIPTransaction* XSIPCommand::createTransaction(XSIPRequest* request, XSIPTransport *transport)
{
	if (m_transaction)
		return m_transaction;
	XSIPTransaction *transaction = new XSIPCommandTransaction();
	XSIPEngine::getCurrentThreadEngine()->addTransaction(transaction);
	XSIPHeader *header = request->getHeader();
	header->addHeader(XSIP_MSG_HEADER_TRANSACTION_ID, transaction->getTransactionId());
	header->addHeader(XSIP_MSG_HEADER_CALL_ID, getCallId());

	transaction->setOwner((XSIPCall*)getObject("XSIPCall"));
	transaction->setTransport(transport);

	request->setTransaction(transaction);
	transaction->setRequest(request);
	m_transaction = transaction;
	return transaction;
}